import HomeData from "@/entity/HomeData";
import { ResponseResult } from "@/entity/ResponseResult";
import { AxiosResponse } from "axios";
import httpClient from "./http-client";
import { db_carousels } from "@/database/carousel";
import { db_product } from "@/database/product";

export class HomeService {
    baseUrl = "/api/v1.0/portal/home";

    public async getData(): Promise<HomeData> {
        const arr: number[] = [];
        const min = Math.min(...db_product.map(it => it.id));
        const max = Math.max(...db_product.map(it => it.id));

        arr.length = 0;
        for (let i = min; i < max; i++) {//一个从0到100的数组
            arr.push(i);
        }
        arr.sort(() => {//随机打乱这个数组
            return Math.random() - 0.5;
        })
        arr.length = 10;//改写长度
        const newProducts = db_product.filter(it => arr.includes(it.id));

        arr.length = 0;
        for (let i = min; i < max; i++) {//一个从0到100的数组
            arr.push(i);
        }
        arr.sort(() => {//随机打乱这个数组
            return Math.random() - 0.5;
        })
        arr.length = 10;//改写长度
        const hotProducts = db_product.filter(it => arr.includes(it.id));

        const homeData: HomeData = {
            "carousels": db_carousels,
            "newProducts": newProducts,
            "hotProducts": hotProducts,
        };

        return Promise.resolve(homeData);
        // return httpClient.get(`${this.baseUrl}/data`).then((res: AxiosResponse<ResponseResult<HomeData>>) => {
        //     if (res.data.data) {
        //         const list = res.data.data;
        //         return Promise.resolve(list);
        //     } else {
        //         throw "no data";
        //     }
        // });
    }
}

const instance = new HomeService();
export default instance;